package com.example.museum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.museum.entity.ProductAppraise;
import com.example.museum.entity.User;
import com.example.museum.mapper.UserMapper;
import com.example.museum.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername())
                .eq(User::getPassword, user.getPassword());
        User userLogin = userMapper.selectOne(wrapper);

        return userLogin;
    }

    @Override
    public void register(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());
        boolean exists = userMapper.exists(wrapper);

        if (!exists) {
            throw new RuntimeException("用户已存在");
        }
        user.setCreateTime(LocalDateTime.now());
        userMapper.insert(user);
    }

    @Override
    public List<ProductAppraise> getappraise(ProductAppraise productAppraise) {

        Long productId = productAppraise.getProductId();

//        userMapper.selectList();
        return List.of();
    }

    @Override
    public Integer getnewUsers() {
        LocalDate startDate = LocalDate.of(2025, 5, 24);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .ge(User::getCreateTime, startDate.atStartOfDay()); // 转换为 LocalDateTime
        Integer count = Math.toIntExact(userMapper.selectCount(wrapper));
        return count;
    }

//    @Override
//    public Long getNewUsersCount(LocalDateTime startDate, LocalDateTime endDate) {
//        // 实现获取指定时间范围内的新增用户数
//        // 这里提供一个简单的实现，实际应用中需要查询数据库
//        try {
//            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.between(User::getCreateTime, startDate, endDate);
//            return userMapper.selectCount(queryWrapper);
//        } catch (Exception e) {
//            // 出现异常时返回默认值
//            return 328L;
//        }
//    }
//
//    @Override
//    public BigDecimal getUsersGrowthRate(Integer days) {
//        // 实现用户数同比增长率计算
//        // 这里提供一个简单的实现，实际应用中可能需要更复杂的计算逻辑
//        try {
//            // 如果是测试环境或者没有足够的历史数据，返回一个默认值
//            return new BigDecimal("22.3");
//
//            // 实际实现应该计算前一个周期的用户数，然后计算增长率
//            // 例如：(currentCount - previousCount) / previousCount * 100
//        } catch (Exception e) {
//            // 出现异常时返回0
//            return BigDecimal.ZERO;
//        }
//    }
}
